package com.school.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

//对应s_subject的所有操作
//返回结果的方式
public class SubjectDao {
	// CRUD
	// 改
	public int update(Subject s) {
		String sql = "update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, s.getSubjectname());
			pstmt.setInt(2, s.getClasshour());
			pstmt.setLong(3, s.getGradeid());
			pstmt.setLong(4, s.getId());
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// 删
	public int del(long id) {
		String sql = "delete from s_subject where id=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setLong(1, id);
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// 增
	public int add(Subject s) {
		String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, s.getSubjectname());
			pstmt.setInt(2, s.getClasshour());
			pstmt.setLong(3, s.getGradeid());
			return pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			return -1;
		} finally {
			DBUtil.close();
		}
	}

	// >>>R
	// 1:查询全部
	public List<Subject> search() {
		List<Subject> datas = new java.util.ArrayList<>();
		try (Connection con = DBUtil.getConnection();
				PreparedStatement ps = con.prepareStatement("select * from s_subject");
				ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datas;
	}

	// 分页查询
	public List<Subject> search(int pageNo, int pageSize) {
		List<Subject> datas = new java.util.ArrayList<>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement ps = con.prepareStatement("select * from s_subject order by id asc limit ?,?;");
			int offset = (pageNo - 1) * pageSize;
			offset = Math.max(offset, 0);
			ps.setInt(1, offset);
			ps.setInt(2, pageSize);
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} // close
		return datas;
	}

	// 查询单个:找不到是null,找到了,返回一个对象
	public Subject get(long id) {
		String sql = "select * from s_subject  where id=?";
		try {
			Connection connection = DBUtil.getConnection();
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setLong(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				return new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid"));
			} else {
				return null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			DBUtil.close();
		}
	}
}
